package com.shxy.web

import com.shxy.response.ResponseData
import org.springframework.web.bind.annotation.ExceptionHandler
import org.springframework.web.bind.annotation.RestControllerAdvice
import org.springframework.web.servlet.NoHandlerFoundException
import javax.servlet.http.HttpServletRequest

/**
 * 自定义异常处理类
 */
@RestControllerAdvice
class GlobalExceptionHandler {

    /**
     * 异常处理函数
     */
    @ExceptionHandler(value = [Exception::class])
    fun noHandlerFoundHandler(request: HttpServletRequest, ex: Exception): ResponseData{
        if(ex is NoHandlerFoundException){
            return ResponseData(404, ex.message)
        }
        return ResponseData(false, -1, ex.message, null)
    }
}